//
//  @ Project : Galaxy Star Trading System
//  @ File Name : PositionDetail.cpp
//  @ Date : 2014/9/5
//  @ Author :
//
//

#include "../StdInc.h"
#include <boost/format.hpp>
using namespace boost;

namespace GSTrader
{
	namespace TradeBase
	{
		CPositionDetail::CPositionDetail() : m_nProviderId(-1), m_nInstrumentId(-1), m_strInstrumentId(""), m_strTransactDate(""), m_strTradingDay(""),
			m_nHedgeFlag(0), m_strTradeID(""), m_dOpenPrice(0), m_nOpenVolume(0), m_nCloseVolume(0), m_dCloseAmount(0), m_dVolume(0), m_nLongShort(0),
			m_dMargin(0), m_dCloseProfitByDate(0), m_dCloseProfitByTrade(0), m_dPositionProfitByDate(0), m_dPositionProfitByTrade(0), m_dPremium(0)
		{
			m_dLastSettlementPrice = 0;
			m_dOpenAmount = 0;

			m_dStopPrice = 0;
			m_dProfitPrice = 0;
		}

		CPositionDetail::~CPositionDetail()
		{

		}

		CPositionDetail::CPositionDetail(CTrade& trade) : CPositionDetail()
		{
			m_nProviderId		= trade.m_nProviderId;
			m_nInstrumentId		= trade.m_nInstrumentID;
			m_strInstrumentId	= trade.m_strInstrumentID;
			m_nLongShort		= BuySellToLongShort(trade.m_nSide, false);
			m_nHedgeFlag		= trade.m_nHedgeFlag;
			m_strTradeID		= trade.m_strTradeID;

			m_strTransactDate	= trade.m_strTradingDay;	
			m_strTradingDay		= trade.m_strTradingDay;
			m_dVolume			= trade.m_nVolume;
			m_dOpenPrice		= trade.m_dPrice;

			m_dMargin			= trade.m_dMargin;
			m_dPremium			= trade.m_dPremium;
		}

		bool CPositionDetail::IsTodayPosition(string strTradingDay)
		{
			if ( m_strTransactDate.empty() || strTradingDay.empty())
			{
				assert(false);
				return false;
			}

			if (strTradingDay != m_strTransactDate) 
				return false;

			return true;
		}

		string CPositionDetail::ToString()
		{
			format fmt(g_language == slChinese ? Chinese::c_szFmt2 : English::c_szFmt2);
			fmt % m_strInstrumentId;
			fmt % m_dVolume;

			return fmt.str();
		}

		string CPositionDetail::GetKey()
		{
			format fmt("%s%d");
			fmt % m_strTradeID;
			fmt % m_nLongShort;

			return fmt.str();
		}

		time_t CPositionDetail::GetTime()
		{
			if (m_strTransactDate.empty())
			{
				assert(false);
				return 0;
			}

			return ConvertTime(m_strTransactDate, "00:00:00");
		}
	}
}